#!/usr/bin/php
<?php

Class Advert_Import
{
#var $production;
var $development;

function Advert_Import()
{
#die("first check query");
## PREPARE CONNECTION ##
/*
$this->production=mysql_connect("localhost","infopark_com_ua","8888zG6Wtv1k",true);
mysql_set_charset ('utf8',$this->production);
mysql_select_db("infopark_com_ua",$this->production);
*/
$this->development=mysql_connect("localhost","infopark_com_ua","8888zG6Wtv1k",true);
mysql_set_charset ('utf8',$this->development);
mysql_select_db("dev_infopark_com_ua1",$this->development);

$this->get_shedule();
}
function get_shedule()
{
$sql="
SELECT s_start.* FROM node n LEFT OUTER JOIN field_data_field_schedule_event s_event ON n.nid = s_event.entity_id LEFT OUTER JOIN field_data_field_schedule_start s_start ON n.nid = s_start.entity_id LEFT OUTER JOIN field_data_field_schedule_place s_place ON n.nid = s_place.entity_id LEFT OUTER JOIN node place_node ON place_node.nid = s_place.field_schedule_place_nid LEFT OUTER JOIN field_data_field_place_city place_city ON place_city.entity_id = s_place.field_schedule_place_nid LEFT OUTER JOIN node event_node ON s_event.field_schedule_event_nid = event_node.nid LEFT OUTER JOIN taxonomy_index ti ON event_node.nid = ti.nid LEFT OUTER JOIN field_data_field_event_ticket_id ticket_id ON event_node.nid = ticket_id.entity_id LEFT OUTER JOIN field_data_field_event_description event_desc ON event_node.nid = event_desc.entity_id LEFT OUTER JOIN field_data_field_event_thumbnail nthumbnail ON event_node.nid = nthumbnail.entity_id LEFT OUTER JOIN file_managed f ON nthumbnail.field_event_thumbnail_fid = f.fid LEFT OUTER JOIN url_alias ua ON ua.source like concat('node/', event_node.nid) 
WHERE (n.type = 'schedule') AND (event_node.type = 'event') 
AND (place_city.field_place_city_tid = 20) 
AND (DATE_FORMAT(s_start.field_schedule_start_value,'%Y%m%d') BETWEEN '20130601' AND '20130631') 

GROUP BY place_node.title, s_start.field_schedule_start_value 
ORDER BY DATE_FORMAT(s_start.field_schedule_start_value,'%Y%m%d') ASC, (ti.tid=111) DESC, s_start.field_schedule_start_value ASC";


$shedules=$this->LIST_Q($sql,$this->development);
foreach($shedules as $shedule)
  {
  $t=strtotime($shedule->field_schedule_start_value);
  $new_start=date('Y',$t).'-'.date('m').'-'.date('d',$t)." ".date('H:m:i');
   $sql1="INSERT INTO field_revision_field_schedule_start 
   (entity_type,bundle,deleted,entity_id,revision_id,language,delta,field_schedule_start_value) 
   VALUES (".$this->Q($shedule->entity_type,1).",".$this->Q($shedule->bundle,1).",".$this->Q($shedule->deleted).",".$this->Q($shedule->entity_id).",".$this->Q($shedule->revision_id).",".$this->Q($shedule->language,1).",".$this->Q($shedule->delta).",".$this->Q($new_start,1).")";
  

    $this->QUERY($sql1,$this->development);
    print $sql1."\n\n";
  }
}
function Q($str,$string=0)
{
return ($string?"'":"").mysql_escape_string($str).($string?"'":"");
}
function QUERY($sql,$conn)
{
$result=mysql_query($sql,$conn);
return $result;
}
function ROW_Q($sql,$conn)
{
$result=$this->QUERY($sql,$conn);
$r=mysql_fetch_object($result);
if(count($r)==0)$r=false;
return $r;
}
function LIST_Q($sql,$conn)
{

$result=$this->QUERY($sql,$conn);
while($r=mysql_fetch_object($result))
  {
  $data[]=$r;
  }
  
  if(count($data)==0)$data=false;
  return $data;
}

}
function pre($array)
{
print_r($array);
}
$AI=new Advert_Import;

?>